﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.ComponentModel;
using System.Windows.Input;
using AliyunMobileTestTechnology.ViewModel.Commands;

namespace AliyunMobileTestTechnology.ViewModel
{
    public class SettingsViewModel
    {
        private static Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

        private static string workSpace = config.AppSettings.Settings["WorkSpace"].Value;
        private static string sourceFileSrc = config.AppSettings.Settings["SourceFileSrc"].Value;
        private static string repositoryUri = config.AppSettings.Settings["RepositoryUri"].Value;

        public static string RepositoryUriConfigValue 
        {
            get { return repositoryUri; }
        }
        public static string SourceFileSrcConfigValue
        {
            get { return sourceFileSrc; }
        }
        public static string WorkSpaceConfigValue
        {
            get { return workSpace; }
        }

        public string WorkSpace 
        {
            set
            {
                workSpace = value;
                RaiseropertyUpdated();
            }
            get
            {
                return config.AppSettings.Settings["WorkSpace"].Value;
            }
        }
        public string SourceFileSrc 
        {
            set 
            {
                sourceFileSrc = value;
                RaiseropertyUpdated();
            }
            get
            {
                return config.AppSettings.Settings["SourceFileSrc"].Value;
            }
        }
        public string RepositoryUri 
        {
            set
            {
                repositoryUri = value;
                RaiseropertyUpdated();
            }
            get
            {
                return config.AppSettings.Settings["RepositoryUri"].Value;
            }
        }

        public bool HasPropertyChanged 
        {
            get 
            {
                return !workSpace.Equals(WorkSpace)||!sourceFileSrc.Equals(SourceFileSrc) || !repositoryUri.Equals(RepositoryUri);
            }
        }

        public ICommand ShowSettings 
        {
            get { return new ShowSettingsCommand(this); }
        }
        public ICommand ApplySettings 
        {
            get { return new ApplySettingsCommand(this); }
        }

        public static string GetSrcPath() 
        {
            return config.AppSettings.Settings["SourceFileSrc"].Value;
        }

        public void ApplyConfigurationChange() 
        {
            config.AppSettings.Settings["WorkSpace"].Value=workSpace;
            config.AppSettings.Settings["SourceFileSrc"].Value= sourceFileSrc;
            config.AppSettings.Settings["RepositoryUri"].Value= repositoryUri;
            config.Save(ConfigurationSaveMode.Full);
            RaiseropertyUpdated();
        }

        public delegate void ShowSettingsEventHandler();

        public event ShowSettingsEventHandler OnShowSettings;

        public void RaiseShowSettings() 
        {
            ShowSettingsEventHandler handler = OnShowSettings;
            if (handler != null) 
            {
                handler.Invoke();
            }
        }

        public delegate void PropertyUpdatedEventHandler();
        public event PropertyUpdatedEventHandler OnPropertyUpdated;

        public void RaiseropertyUpdated() 
        {
            PropertyUpdatedEventHandler handler = OnPropertyUpdated;
            if (handler != null) 
            {
                handler.Invoke();
            }
        }
    }
}
